//
//  XYHomeViewController.m
//  XYSfxyxuSwift
//
//  Created by 许洋 on 2018/4/4.
//  Copyright © 2018年 许洋. All rights reserved.
//

#import "XYHomeViewController.h"
#import "XYSfxyxuSwift-Swift.h"

@interface XYHomeViewController ()

@end

@implementation XYHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    @weakify(self)
    [[self rac_signalForSelector:@selector(tableView:didSelectRowAtIndexPath:) fromProtocol:@protocol(UITableViewDelegate)] subscribeNext:^(RACTuple * _Nullable x) {
        NSIndexPath *indexPath = x.second;
        XYHomeModel *model = self.dataSource[indexPath.row];
        Class class = NSClassFromString(model.controller);
        UIViewController *vc = (UIViewController *)[class new];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [[RACObserve(self, dataSource)deliverOnMainThread] subscribeNext:^(id  _Nullable x) {
        @strongify(self)
        // 刷新数据
        [self.tableView reloadData];
    }];
  
    NSArray *dataArr = @[@{@"title":@"图片美白",
                           @"controller":@"XYImageProcessController"},
                         @{@"title":@"交换方法",
                           @"controller":@"XYImageViewController"},
                         @{@"title":@"ReactiveCocoa",
                           @"controller":@"XYRACTableViewController"},
                         @{@"title":@"ZFPlayer",
                           @"controller":@"XYPlayerViewController"},
                         @{@"title":@"AliyunPlayer",
                           @"controller":@"XYAliyunPlayerController"},
                         @{@"title":@"GPUImage",
                           @"controller":@"XYGPUImageController"},
                         @{@"title":@"DownloadVideos",
                           @"controller":@"XYDownloadController"},
                         @{@"title":@"VideosList",
                           @"controller":@"XYVideosListController"},
                         @{@"title":@"PrograssHUD",
                           @"controller":@"CFPrograssHUDController"},
                         @{@"title":@"FMDB",
                           @"controller":@"XYFMDBViewController"}];
    self.dataSource = [XYHomeModel mj_objectArrayWithKeyValuesArray:dataArr];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    XYHomeModel *model = self.dataSource[indexPath.row];
    cell.textLabel.text = model.title;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
